﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using StockAdjust;
using System.Collections;
using System.Transactions;

namespace BusinessLogicLayer
{
    public class ApproveStockAdjustmentVoucherControl
    {
        AdjustmentRepository adjustmentRepo;
        AdjustmentDetailRepository adjustmentDetailRepo;
        UserRepository userRepo;
        EmployeeRepository employeeRepo;
        StationeryRepository stationeryRepo;
        StockHistoryRepository stockHistoryRepo;

        public ApproveStockAdjustmentVoucherControl()
        {
            adjustmentRepo = new AdjustmentRepository();
            adjustmentDetailRepo = new AdjustmentDetailRepository();
            userRepo = new UserRepository();
            employeeRepo = new EmployeeRepository();
            stationeryRepo = new StationeryRepository();
            stockHistoryRepo = new StockHistoryRepository();
        }

        public List<AdjustmentDisplay> GetAllPendingAdjustmentForSupervisor()
        {
            List<Adjustment> supervisorList = adjustmentRepo.GetAllPendingAdjustmentForSupervisor();
            List<AdjustmentDisplay> list = FormatAdjustmentList(supervisorList);
            return list;
        }

        public List<AdjustmentDisplay> GetAllPendingAdjustmentForStoreManager()
        {
            List<Adjustment> managerList = adjustmentRepo.GetAllPendingAdjustmentForStoreManager();
            List<AdjustmentDisplay> list = FormatAdjustmentList(managerList);
            return list;
        }

        private List<AdjustmentDisplay> FormatAdjustmentList(List<Adjustment> adjustmentList)
        {
            List<AdjustmentDisplay> list = new List<AdjustmentDisplay>(adjustmentList.Count);
            adjustmentList = adjustmentList.OrderBy(x => x.AdjVoucherID).ToList();
            for (int i = 0; i < adjustmentList.Count; i++)
            {
                AdjustmentDisplay adjDisplay = new AdjustmentDisplay();
                adjDisplay.AdjVoucherId = adjustmentList[i].AdjVoucherID;
                adjDisplay.AdjVoucherIdFormat = FormatVoucherNo(adjustmentList[i]);
                adjDisplay.RequestedBy = adjustmentList[i].Employee.EmployeeName;
                adjDisplay.RequestedDate = adjustmentList[i].RequestedDate.Value.ToShortDateString();
                list.Add(adjDisplay);
            }
            return list;
        }

        public User GetUserByUsername(string username)
        {
            return userRepo.GetUserByUsername(username);
        }

        public Employee GetEmployeeByUserId(int userId)
        {
            return employeeRepo.GetEmployeeByUserId(userId);
        }

        public string UpdateVouchers(User user, List<int> voucherList)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    for (int i = 0; i < voucherList.Count; i++)
                    {
                        Adjustment adjustment = adjustmentRepo.GetAdjustmentByAdjVoucherId(voucherList[i]);
                        adjustment.ApprovalBy = employeeRepo.GetEmployeeByUserId(user.UserID).EmployeeID;
                        adjustment.ApprovalDate = DateTime.Now;
                        adjustment.Status = "Approved";
                        adjustmentRepo.UpdateAdjustment(adjustment);

                        List<AdjustmentDetail> adjustmentDetail = adjustmentDetailRepo.GetAdjustmentDetailByVoucherId(voucherList[i]);
                        for (int j = 0; j < adjustmentDetail.Count; j++)
                        {
                            // Update StockHistory
                            int stockHistoryId = (int)adjustmentDetail[j].StockHistoryID;
                            StockHistory stockHistory = stockHistoryRepo.GetStockHistoryByStockHistoryID(stockHistoryId);
                            stockHistory.Date = DateTime.Now;
                            stockHistoryRepo.UpdateStockHistory(stockHistory);
                        }
                    }  
                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
            }

            // Redirect to successful page
            string redirectURL = "/StockAdjustmentApproved.aspx";
            return redirectURL;
        }

        public string ViewAdjustmentDetail() 
        {
            string redirectURL = "/ApproveStockAdjustmentVoucher.aspx";
            return redirectURL;            
        }

        public List<AdjustmentDetail> DisplayAdjustmentDetail(int voucherId)
        {
            return adjustmentDetailRepo.GetAdjustmentDetailByVoucherId(voucherId); 
        }

        public string ReturnToStockAdjustmentVoucherList()
        {
            string redirectURL = "/ApproveStockAdjustmentVoucherList.aspx";
            return redirectURL;
        }

        private string FormatVoucherNo(Adjustment adjustment)
        {
            string voucherNo = "";
            string departmentID = adjustment.Employee.DeptID;
            string year = DateTime.Today.Year.ToString().Substring(2, 2);
            voucherNo = departmentID + "/" + adjustment.AdjVoucherID + "/" + year;
            return voucherNo;
        }
        
    }
}
